package vn.kiemviec.app.fragments;

import java.util.ArrayList;

import vn.kiemviec.app.R;
import vn.kiemviec.app.data.JobEntry;
import vn.kiemviec.app.task.CBTask.CBTaskListener;
import vn.kiemviec.app.task.SearchJobTask;
import vn.kiemviec.app.utils.LogTools;
import vn.kiemviec.app.viewadapter.SearchAdapter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class SearchResultFragment extends BaseFragment {

	private Button btnBack;
	private Button btnSearchAgain;
	
	private ListView lsvSearch;
	private String mIndustryId ="";
	private String mKeyword;
	
	private ArrayList<JobEntry> listJobs;
	
	public static  SearchResultFragment newInstance(String id){
		SearchResultFragment fr = new SearchResultFragment();
		fr.setIndustry(id);
		return fr;
	}

	public static  SearchResultFragment newSearchByWord(String keyword){
		SearchResultFragment fr = new SearchResultFragment();
		fr.setKeyword(keyword);
		return fr;
	}

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	public void setIndustry(String id){
		mIndustryId = id;
	}
	
	public void setKeyword(String keyword){
		mKeyword = keyword;
	}
	
	@Override
	public int OnGetLayoutResID() {
		// TODO Auto-generated method stub
		return R.layout.frag_search_result;
	}
	
//	public void set
	@Override
	public void onSetUpView() {
		// TODO Auto-generated method stub
		lsvSearch		= findView(R.id.lsvSearch);
		btnBack			= findView(R.id.btnLogin);
		btnSearchAgain	= findView(R.id.btnSearchAgain);
		
		if(mIndustryId.length() == 0){
			loadDataByKeyWord(mKeyword);
		}else{
			loadDataByIndustry(mIndustryId);
		}
		
	}

	@Override
	public void onSetUpEventHandler() {
		// TODO Auto-generated method stub
		btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				NavParrent.popFragment();
//				popFragment();
			}
		});
		btnSearchAgain.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		
		lsvSearch.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String jobId = listJobs.get(arg2).JobID;
				LogTools.info("jobid "+jobId);
				NavParrent.pushFragment(JobDetailsFragment.newInstance(jobId));
			}
		});
	}
	
	private void loadDataByIndustry(String id){
		
		SearchJobTask taks = new SearchJobTask(getActivity(),SearchJobTask.SEARCH_BY_INDUSTRY,id);
		taks.setOnTaskListener(new CBTaskListener<JobEntry>() {

			@Override
			public void onSuccessfull(ArrayList<JobEntry> listEntry) {
				// TODO Auto-generated method stub
				LogTools.info("total:"+listEntry.size()+"jobs");
				listJobs = listEntry;
				SearchAdapter adapter = new SearchAdapter(getActivity(), listEntry);
				lsvSearch.setAdapter(adapter);
			}

			@Override
			public void onError(int error) {
				// TODO Auto-generated method stub
				
			}
		});
		taks.execute();
	}
	
	private void loadDataByKeyWord(String keyword){
		
		SearchJobTask taks = new SearchJobTask(getActivity(),SearchJobTask.SEARCH_BY_KEYWORD,keyword);
		taks.setOnTaskListener(new CBTaskListener<JobEntry>() {

			@Override
			public void onSuccessfull(ArrayList<JobEntry> listEntry) {
				// TODO Auto-generated method stub
				LogTools.info("total:"+listEntry.size()+"jobs");
				SearchAdapter adapter = new SearchAdapter(getActivity(), listEntry);
				lsvSearch.setAdapter(adapter);
			}

			@Override
			public void onError(int error) {
				// TODO Auto-generated method stub
				
			}
		});
		taks.execute();
	}
	
	

}
